package leetcode.Top100;

import leetcode.ListNode;

/**
 * 142. 环形链表 II
 */
public class Top051_LC142 {
    public ListNode detectCycle(ListNode head) {
        ListNode a = head;
        ListNode b = head;
        while (true) {
            if (b==null || b.next==null) {return null;}
            a = a.next;
            b = b.next.next;
            if (a == b) {
                break;
            }
        }
        b = head;
        while (a != b) {
            a = a.next;
            b = b.next;
        }
        return b;
    }
}
